/*
 * ActivityView.java
 */

package procrastinator;

import procrastinator.table.ActivityTable;
import procrastinator.autocomplete.ValueChangedListener;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class JProcrastinatorFrame extends FrameView implements ValueChangedListener {
    private Date previousAddition = null;
    private ActivityTable model;
    private CategoryPredictor predictor;

    public JProcrastinatorFrame(SingleFrameApplication app) {
        super(app);
        
        DatabaseConnection conn = Main.getApplication().getConnection();
        
        model = new ActivityTable(conn);
        predictor = new CategoryPredictor(conn);

        initComponents();
        
        jActivityTable1.setModel(model);
        jActivityTable1.updateRenderers(conn.getCategoryNames());
        
        for( String categoryName : conn.getCategoryNames() ) {
            jComboBox2.addItem(categoryName);
        }
        
        getFrame().getRootPane().setDefaultButton(jButton2);
        jAutocompleteTextField1.addValueChangedListener(this);

        // status bar initialization - message timeout, idle icon and busy animation, etc
        //ResourceMap resourceMap = getResourceMap();
        
        previousAddition = new Date();

    }
    
    public void initFocus() {
        // must initialize focus after window is visible
        jAutocompleteTextField1.requestFocusInWindow();
    }

    @Action
    public void showAboutBox(ActionEvent e) {
        if (aboutBox == null) {
            JFrame mainFrame = Main.getApplication().getMainFrame();
            aboutBox = new ProcrastinatorAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Main.getApplication().show(aboutBox);
    }
    
    private void addToTable() {
        Date now = new Date();
        
        model.addRow(previousAddition, now, jAutocompleteTextField1.getText(), (String) jComboBox2.getSelectedItem());
        previousAddition = now;
        jAutocompleteTextField1.addValue(jAutocompleteTextField1.getText());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jAutocompleteTextField1 = new procrastinator.autocomplete.JAutocompleteTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jActivityTable1 = new procrastinator.table.JActivityTable();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(procrastinator.Main.class).getContext().getResourceMap(JProcrastinatorFrame.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox2.setName("jComboBox2"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetPreviousTime(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitFromButton(evt);
            }
        });

        jAutocompleteTextField1.setText(resourceMap.getString("jAutocompleteTextField1.text")); // NOI18N
        jAutocompleteTextField1.setName("jAutocompleteTextField1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jActivityTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jActivityTable1.setName("jActivityTable1"); // NOI18N
        jScrollPane1.setViewportView(jActivityTable1);

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jAutocompleteTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 217, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton1))
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jButton1)
                    .add(jButton2)
                    .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jAutocompleteTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(procrastinator.Main.class).getContext().getActionMap(JProcrastinatorFrame.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showReport(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void submitFromButton(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitFromButton
        addToTable();
        jAutocompleteTextField1.setText("");
        jAutocompleteTextField1.requestFocusInWindow();
    }//GEN-LAST:event_submitFromButton

    private void resetPreviousTime(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetPreviousTime
        previousAddition = new Date();
    }//GEN-LAST:event_resetPreviousTime

    private void showReport(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showReport
        PieReportViewer.showReport(model);
    }//GEN-LAST:event_showReport

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private procrastinator.table.JActivityTable jActivityTable1;
    private procrastinator.autocomplete.JAutocompleteTextField jAutocompleteTextField1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;

    public void valueChanged(String newValue) {
        String predictedCategory = predictor.predict(newValue);
        
        if( predictedCategory != null ) {
            jComboBox2.setSelectedItem(predictedCategory);
        }
    }
    
    
}
